import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Pannable extends StatefulWidget {
  const Pannable({super.key,
    this.alignment=Axis.horizontal,
    required this.child1,
    required this.child2,
    this.child1Value=100,
    this.minValue=20,
    this.separatorSpace=6});

  final Axis alignment;
  final Widget child1;
  final Widget child2;
  final double child1Value;
  final double minValue;
  final double separatorSpace;

  @override
  State<Pannable> createState() => _PannableState();
}

class _PannableState extends State<Pannable> {

  double parentValue = -1;
  late double child1Value;
  late double minValue;

  @override
  void initState() {
    minValue = widget.minValue;
    child1Value = widget.child1Value;
    super.initState();
  }

  void onResize(double delta) {
    setState(() {

      double nowMinWidth = minValue*2 < parentValue ? minValue : parentValue / 2;

      if (child1Value <= nowMinWidth && delta < 0) {
        child1Value = nowMinWidth;
      } else if (parentValue - child1Value <= nowMinWidth && delta > 0) {
        child1Value = parentValue - nowMinWidth;
      } else {
        child1Value += delta;
      }
    });
  }

  @override
  Widget build(BuildContext context) {

    List<Widget> containerChildren = [
      SizedBox(
          width: widget.alignment==Axis.horizontal ? child1Value : null,
          height: widget.alignment==Axis.horizontal ? null : child1Value,
          child: widget.child1),
      ResizableBar(onResize: onResize, separateSpace: widget.separatorSpace, alignment: widget.alignment,),
      Expanded(child: widget.child2)
    ];

    return LayoutBuilder(builder: (context, constrains) {
      parentValue = widget.alignment==Axis.horizontal ? constrains.maxWidth : constrains.maxHeight;
      return widget.alignment==Axis.horizontal ?
      Row(children: containerChildren) : Column(children: containerChildren);
    });
  }
}


class ResizableBar extends StatefulWidget {
  final void Function(double) onResize;
  final Axis alignment;
  final double separateSpace;

  ResizableBar({required this.onResize, this.alignment = Axis.horizontal, required this.separateSpace});

  @override
  _ResizableBarState createState() => _ResizableBarState();
}

class _ResizableBarState extends State<ResizableBar> {
  double _startX = 0.0;

  void _onPanUpdate(DragUpdateDetails details) {
    double delta;
    if (widget.alignment == Axis.horizontal) {
      delta = details.localPosition.dx - _startX;
      _startX = details.localPosition.dx;
    } else {
      delta = details.localPosition.dy - _startX;
      _startX = details.localPosition.dy;
    }
    widget.onResize(delta);
  }

  @override
  Widget build(BuildContext context) {
    return MouseRegion(
      cursor:
      widget.alignment == Axis.horizontal ?
      SystemMouseCursors.resizeColumn : SystemMouseCursors.resizeRow,
      child: GestureDetector(
          onPanStart: (details) {
            _startX = widget.alignment == Axis.horizontal ?
            details.localPosition.dx : details.localPosition.dy;
          },
          onPanUpdate: _onPanUpdate,
          child:
          widget.alignment == Axis.horizontal ?
          Container(width: widget.separateSpace, color: Colors.transparent,) : Container(height: widget.separateSpace, color: Colors.transparent,)
      ),
    );
  }
}